package com.cx.controller;


import com.cx.entity.User;
import com.cx.service.IUserService;
import com.cx.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cx
 * @since 2024-06-07
 */
@CrossOrigin    //解决跨域问题注解
@RestController //返回UserJson格式数据接口
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("login")
    public R loginById(@RequestParam("username") String username,
                       @RequestParam("password") String password,
                       HttpSession session) {
//        转化为Map格式传递参数
        Map<String, Object> person = new HashMap<String, Object>();
        person.put("username", username);
        person.put("password", password);
//            判断用户登录用户的rid是否等于2
        person.put("rid", 2);
        session.setAttribute("userHome", username);
//        System.out.println("person = " + person);
        return userService.userLogin(person);
    }

    //    添加用户
    @RequestMapping("save")
    public R userSave(@RequestParam("username") String username,
                      @RequestParam("password") String password,
                      @RequestParam("phone") String phone) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhone(phone);
        return R.ok().put("data", userService.save(user));
    }

    //    修改用户
    @RequestMapping("Update")
    public R userUpdate(User user) {
        return R.ok().put("data", userService.updateById(user));
    }

    //    删除用户
    @RequestMapping("del")
    public R delUser(@PathParam("id") Integer id) {
        return R.ok().put("data", userService.removeById(id));
    }

    //    查询全部用户信息
    @RequestMapping("list")
    public R getUserAll() {
        return R.ok().put("data", userService.list());
    }


}
